<template>
    <div>
        <bar1 title="注册"></bar1>
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" placeholder="手机号"
                    :rules="[{ pattern, message: '请填写正确的手机号' }]" />

                <!-- <van-field  label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写验证码' }]" /> -->
                <van-field v-model="text" type="text" name="password" placeholder="请输入验证码">
                    <template #button>
                        <van-button size="small" type="primary" @click="getpas()">{{ time > 0 ? time + 's秒后重新获取' :
                            '获取验证码' }}</van-button>
                    </template>
                </van-field>

                <van-radio-group v-model="checked">
                    <van-radio name="1">
                        <p> 已阅读并同意《乐友服务协议》、《隐私保护协议》 及 《儿童个人信息保护规则》</p>
                    </van-radio>
                </van-radio-group>

            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    注册
                </van-button>
            </div>
        </van-form>

    </div>
</template>

<script setup>
import bar1 from '@/components/bar1.vue';
import { ref } from 'vue';
import { registerApi } from '@/api/api';
const username = ref('');
const password = ref('');

import { showToast } from 'vant';
import router from '@/router';
const onSubmit = (values) => {
    console.log('submit', values);
    registerApi(username.value, password.value).then((res) => {
        console.log(res);
        if (res.data.code == 0) {
            showToast(res.data.msg);
            router.push('/login')
        } else {
            showToast(res.data.msg);
        }
    })
};

const pattern = /1[3-6]\d{9}$/
const checked = ref('0');
let time = ref(0)

const getpas = () => {
    time.value = 10
    let timer = setInterval(() => {
        time.value--

        if (time.value == 0) {
            clearInterval(timer)
        }
        
    }, 1000);
    getitems()
}
let text = ref('')
const getitems = () => {
    text.value = Math.random().toString().slice(2,8)
}

</script>

<style lang="scss" scoped></style>